package com.yeraphael.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeraphael.annotation.Log;
import com.yeraphael.entity.*;
import com.yeraphael.mapper.EmpExprMapper;
import com.yeraphael.mapper.EmpLogMapper;
import com.yeraphael.mapper.EmpMapper;
import com.yeraphael.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogMapper empLogMapper;

    @Override
    public PageBeandf page(EmpQueryParam param) {
//        Long total =  empMapper.total();
//        Integer start = (page - 1) * pageSize;
//        List<Emp> rows = empMapper.rows(start, pageSize);

//        设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());

//        调用mapper的列表查询方法
        List<Emp> empList = empMapper.list(param);
        Page p = (Page) empList;

        return new PageBeandf(p.getTotal(), p.getResult());
    }

    @Transactional(rollbackFor = Exception.class)  //开启事务
    @Override
    public void save(Emp emp) {
        try {
//        1.保存员工的基本信息到emp表
            emp.setPassword("123456");
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());

            empMapper.saveEmp(emp);

            log.info(String.valueOf(emp.getId()));

//        2.批量保存员工的工作经历信息
//        for( int i = 0; i < emp.getExprList().size(); i++ ) {
//            EmpExpr empExpr = emp.getExprList().get(i);
//            empExpr.setEmpId(emp.getId());
//            empExprMapper.save(empExpr);
//        }
            Integer id = emp.getId();


            List<EmpExpr> empExpr = emp.getExprList();
            if( !CollectionUtils.isEmpty(empExpr) ) {
                empExpr.forEach(empExpr1 -> {
                    empExpr1.setEmpId(id);
                });
            }

            empExprMapper.insertBatch(empExpr);
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("插入员工信息："+ emp);
            empLogMapper.insert(empLog);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch( List<Integer> ids) {
        try {
//        1.删除员工信息
            empMapper.deleteBatch(ids);
//        2.删除工作经历信息
            empExprMapper.deleteBatch(ids);
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("删除员工id列表："+ ids);
            empLogMapper.insert(empLog);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Emp getEmp(Integer id) {
        Emp emp = null;
        try {
//        1.查询该员工的工作经历
            List<EmpExpr> exprList = empExprMapper.getEmpExpr(id);
//         2.查询该员工的个人信息
            emp = empMapper.getEmp(id);
            emp.setExprList(exprList);

//            联表查询
            emp = empMapper.getEmp(id);
            return emp;
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("查询id为"+ id+"员工的个人信息"+ emp);
            empLogMapper.insert(empLog);
        }

    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        try {
            empExprMapper.deleteById(emp.getId());
//        1.修改该员工的工作经历
            List<EmpExpr> exprList = emp.getExprList();
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            if( !CollectionUtils.isEmpty(exprList) ) {
                empExprMapper.insertBatch(exprList);
            }

//         2.修改该员工的个人信息
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.updateEmp(emp);
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("修改id为"+ emp.getId()+"员工的个人信息"+ emp);
            empLogMapper.insert(empLog);
        }
    }

    @Override
    public List<Emp> getListCl() {
        List<Emp> empList = empMapper.getListCl();
        return empList;
    }



}
